﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Logic.BaseContent;
using Job.Web.Framework;

namespace Job.Web.userControls.Admin
{
    public partial class AdminUserList : System.Web.UI.UserControl
    {
        AdminUserLogic logic = new AdminUserLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindCount();
                BindData();
            }
        }

        public void BindCount()
        {
            pagerButtom.RecordCount = logic.GetCount();
            pagerButtom.DataBind();
        }
        public void BindData()
        {
            int pageSize = pagerButtom.PageSize;
            int pageIdx = pagerButtom.CurrentPageIndex;
            this.gridUsers.DataSource = logic.GetUserPageList(pageSize, pageIdx);
            this.gridUsers.DataBind();
        }

        protected void gridUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    LinkButton lbtnDelete = (LinkButton)e.Row.Cells[5].Controls[0];
                    LinkButton lbtnEdit = (LinkButton)e.Row.Cells[4].Controls[0];
                    string displayLogin = e.Row.Cells[0].Text.Trim().ToLower();
                    if (displayLogin.Equals("admin"))
                    {
                        lbtnDelete.Visible = false;
                        lbtnEdit.Visible = false;
                    }
                    else
                    {
                        if (lbtnDelete != null && lbtnDelete.Text.Equals("删除"))
                            lbtnDelete.Attributes.Add("onClick", "return confirm('是否确认删除？')");
                    }
                }
            }
        }

        protected void gridUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int row = e.RowIndex;
            Label label = (Label)gridUsers.Rows[row].FindControl("lbUserId");
            string userId = label.Text;
            if ( null == userId || userId.Trim().Equals("")) return;
            logic.DeleteById(Convert.ToInt32(userId));

            BindCount();
            BindData();
        }

        protected void gridUsers_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int row = e.NewEditIndex;
            Label label = (Label)gridUsers.Rows[row].FindControl("lbUserId");
            string userId = label.Text;
            if (null == userId || userId.Trim().Equals("")) return;
            string url = "AdminMain.aspx?controlType="
                + Convert.ToString((int)Constants.SubjectType.AdminManagerInfo)
                + "&userId=" + userId;

            Response.Redirect(url);

        }

        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btNew_Click(object sender, EventArgs e)
        {
            string url = "AdminMain.aspx?controlType="
                + Convert.ToString((int)Constants.SubjectType.AdminManagerInfo);

            Response.Redirect(url);
        }
    }
}